<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: AddBrand.php
 * Date: 2017/2/7
 * Time: 23:20
 */

namespace app\home\model\page\shop;

use app\api\model\dao\GoodsBrands;
use app\library\Error;
use app\library\PageBase;
use think\Log;
use think\Validate;

class AddBrand extends PageBase
{
    private $objModel;

    public function __construct()
    {
        parent::__construct();
        $this->objModel = new GoodsBrands();
    }

    public function excute($param)
    {

        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        $ret = $this->checkParam($param);
        if (true !== $ret) {
            return $this->getRes(Error::ERR_PARAM, $ret . json_encode($param));
        }
        //var_dump($param);
        $ret = $this->objModel->addGoodsBrand($param['brand_name'], $param['logo'], $param['is_show'], $param['display_order']);
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }
        return $this->getRes();

    }

    public function checkParam($param)
    {
        $rule = [
            'brand_name' => 'require|max:25',
            'is_show' => 'require',
            'display_order' => 'require',
        ];
        $msg = [
            'brand_name.require' => '名称必须',
            'brand_name.max' => '名称最多不能超过25个字符',
            'is_show.require' => 'is_show必须',
            'display_order.require' => 'display_order必须',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return $result;
    }
}
